layui.config({
    base: basePath,
    version: skyeyeVersion
}).extend({
    window: 'js/winui.window'
}).define(['window', 'jquery', 'winui'], function (exports) {
    winui.renderColor();
    var $ = layui.$;
    var tenantId = GetUrlParam("tenantId");

    // 刷新按钮
    $("body").on("click", "#reloadTable", function () {
        loadAndRender();
    });

    // 首次加载
    loadAndRender();

    function loadAndRender() {
        AjaxPostUtil.request({
            url: reqBasePath + 'queryTenantOrderStatistics',
            params: { tenantId: tenantId },
            type: 'json',
            callback: function (res) {
                var rows = res.rows || [];
                var bean = res.bean || {};
                $("#totalPrice").text(bean.totalPrice || '0');
                $("#totalCount").text(bean.totalCount || '0');
                $("#appCount").text(bean.appCount || '0');
                renderChart(rows);
            }
        });
    }

    function renderChart(rows) {
        var chartDom = document.getElementById('paymentSummaryChart');
        var myChart = echarts.init(chartDom);
        var xData = rows.map(item => item.oddNumber);
        var yData = rows.map(item => parseFloat(item.allPrice));
        var statusData = rows.map(item => item.state);
        var statusColor = statusData.map(function (s) {
            if (s === 'pass') return '#52c41a';
            if (s === 'inExamine') return '#faad14';
            return '#ff4d4f';
        });
        var statusText = statusData.map(function (s) {
            if (s === 'pass') return '已通过';
            if (s === 'inExamine') return '审核中';
            return '其他';
        });
        var option = {
            title: { text: '订单金额统计' },
            tooltip: {},
            grid: { left: 60, right: 20, top: 60, bottom: 40 },
            xAxis: {
                type: 'category',
                data: xData
            },
            yAxis: { type: 'value' },
            series: [{
                name: '金额',
                type: 'bar',
                data: yData,
                itemStyle: {
                    color: function (params) {
                        return statusColor[params.dataIndex];
                    }
                },
                label: {
                    show: true,
                    position: 'top',
                    formatter: function (params) {
                        return statusText[params.dataIndex];
                    },
                    color: function (params) {
                        return statusColor[params.dataIndex];
                    },
                    fontWeight: 'bold',
                    fontSize: 20
                }
            }]
        };
        myChart.setOption(option);
    }

    window.onresize = function () {
        if (document.getElementById('paymentSummaryChart')) {
            echarts.init(document.getElementById('paymentSummaryChart')).resize();
        }
    };

    exports('paymentStats', {});
});
